#!/usr/bin/env node

'use strict';
const path = require('path');
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require('fs'),{suffix: "Blue"});

const logger = require('tracer').colorConsole();
const _ = require('lodash');
const rj = require("./utils/readjson");


let local = process.cwd();

let mainPackObj = {};
let parentPackObj = {};
let brothersPackObj = [];


exports.anyCurrent = function (cupath) {
  let parPack = cupath + "/package.json";
  let parpath = "";
  //判断当前目录中是否是npm项目
  fs.accessBlue(parPack,fs.constants.R_OK).then( (err) =>{
    logger.info(err ? '当前目录下没有"package.json"文件!' : '准备分析"package.json"文件...');
    if(!err){
      //获取当前项目的packagejson 对象，保存到缓存中，并不进行分析
      mainPackObj = rj.robj(parPack);
      let par = mainPackObj['parent'];
      //判断parent对象是否存在设定值
      if(par && par !== "" && par['path'] !== undefined && par['path'] !== ""){
        parpath = path.join(cupath, par['path'], "/package.json");
        return fs.accessBlue(parpath,fs.constants.R_OK)
      }
    }
  }).then((err)=>{
    //分析父节点将父节点信息纳入构建树
    logger.info(err ? '父节点没有"package.json"文件!' : '准备分析父节点"package.json"文件...');
    if(!err){
      let parentPackObj= rj.robj(parpath);
      buildTargetDepend(parentPackObj['dependencies']);

    }
  })

  // fs.access(parPack, fs.constants.R_OK, (err) => {
  //   console.log(err ? '当前目录下没有"package.json"文件!' : '准备分析"package.json"文件...');
  //
  //   if (!err) {
  //     mainPackObj = rj.robj(cupath + "/package.json");
  //     // console.log(mainPackObj);
  //     //判断是否存在父节点
  //     let par = mainPackObj['parent'];
  //     if (par && par !== "" && par['path'] !== undefined && par['path'] !== "") {
  //       let parpath = path.join(cupath, par['path'], "/package.json");
  //       parentPackObj = rj.robj(parpath);
  //       // console.log(parentPackObj);
  //     }
  //     //判断是否存在兄弟节点
  //     let models = mainPackObj['models'];
  //     if (models != undefined) {
  //       for (let temp in models) {
  //         let tpth = path.join(cupath, models[temp], "/package.json");
  //         brothersPackObj.push(rj.robj(tpth));
  //         // console.log(models[temp]);
  //       }
  //       console.log(mainPackObj);
  //       console.log(parentPackObj);
  //       console.log(brothersPackObj);
  //       getAnyDepend(parentPackObj,mainPackObj);
  //       getAnyDevDepend(parentPackObj,mainPackObj);
  //
  //       _.each(brothersPackObj,(va)=>{
  //         getAnyDepend(va,mainPackObj);
  //         getAnyDevDepend(va,mainPackObj);
  //       });
  //     }
  //   }
  // });
}

function getAnyDepend(parobj,mainObj){

  let pdepend = _.keys(parobj['dependencies']);
  let mdepend = _.keys(mainObj['dependencies']);
  console.log("直接依赖相似组件为"+_.intersection(pdepend,mdepend).join(","));

}

function getAnyDevDepend(parobj,mainObj){

  let pdepend = _.keys(parobj['devDependencies']);
  let mdepend = _.keys(mainObj['devDependencies']);
  console.log("开发依赖相似组件为"+_.intersection(pdepend,mdepend).join(","));

}

let targetBuildDependTree={};
let targetBuildDevDependTree={};


//围绕父节点开始构建
function buildTargetDevDepend(){

}

function buildTargetDepend(obj) {
  let oldDep = _.keys(targetBuildDependTree);
  let newDep = _.keys(obj);
  let res =_.intersection(targetBuildDependTree,obj);
  if(res.length>0){
    console.log("存在冲突包请查看："+res.join(","));
  }else {
    _.assign(targetBuildDependTree,targetBuildDependTree,obj);
  }
  console.log(targetBuildDependTree);
}
